﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Permissions;

namespace Memorize_Game
{
    internal partial class frm_topscorer : Form
    {
        FileStream FS; BinaryReader BR; BinaryWriter BW; int comptime; string pname; bool writeFlag; DialogResult DR; char OPT; string filePath;
        

        internal frm_topscorer(char opt)
        {
            InitializeComponent(); OPT = opt;
            if (OPT == 'V')
            {
                btn_save.Text = "Close"; this.txtbx_pname.ReadOnly = true; this.loadInfo(); txtbx_pname.Cursor = DefaultCursor;
            }
        }

        #region IO operations

        private void openFile() //open file
        {
            filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\NIIT Ghana\\TSI.DAT";
            try
            {
                FS = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
            }
            catch (IOException IOE) { MessageBox.Show(IOE.Message); }
        }

        private int readFile() //read from file
        {
            this.openFile();
            try
            {
                BR = new BinaryReader(FS); pname = BR.ReadString(); comptime = BR.ReadInt32(); BR.Close();
            }
            catch (IOException IOE) { MessageBox.Show(IOE.Message); }
            this.closeFile(); return comptime;
        }

        private bool writeFile() //write to file
        {
            writeFlag = true;
            this.openFile();
            try
            {
                BW = new BinaryWriter(FS); BW.Write(pname); BW.Write(comptime); BW.Flush(); BW.Close();
            }
            catch (IOException IOE) { MessageBox.Show(IOE.Message); writeFlag = false; }
            this.closeFile();
            return writeFlag;
        }

        private void closeFile() //close file
        {
            try
            {
                FS.Close();
            }
            catch (IOException IOE) { MessageBox.Show(IOE.Message); }
        }

        private void loadInfo() //load info to controls
        {
            this.readFile(); txtbx_pname.Text = pname; lbl_comptime.Text = Convert.ToString(comptime);
        }

        #endregion

        private void btn_save_Click(object sender, EventArgs e) //save button click
        {
            frm_playboard.menuClick.Play();
            if (OPT == 'V')
            {
                this.Close();
            }
            else
            {
                pname = txtbx_pname.Text.Trim(); comptime = int.Parse(lbl_comptime.Text.Trim());
                if (this.writeFile())
                {
                    DR = MessageBox.Show("Information saved successfully !", "Top Scorer Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (DR == DialogResult.OK)
                    {
                        this.Close();
                    }
                }
            }
        }

        internal int lowesTime
        {
            get
            {
                return this.readFile();
            }
            set
            {
                lbl_comptime.Text = Convert.ToString(value);
            }
        }

        private void btn_save_MouseDown(object sender, MouseEventArgs e)
        {
            btn_save.ForeColor = Color.OrangeRed;
        }

        private void btn_save_MouseUp(object sender, MouseEventArgs e)
        {
            btn_save.ForeColor = Color.DarkOrange;
        }
    }
}
